package com.weishaoying.thrift.learn.service;


import com.weishaoying.thrift.learn.entity.User;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.Assert;

import java.net.SocketTimeoutException;
import java.util.List;

public class Client {

    private static final Logger logger = LoggerFactory.getLogger(Client.class);

    private static UserService.Iface client;

    public static void main(String[] args) throws InterruptedException {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("client.xml");
        client = (UserService.Iface) beanFactory.getBean("clientProxy");

        Thread.sleep(5000);

        //***************************
        //此处实现你的方法调用
        User user = null;
        try {
            user = client.getUserById(12);

            System.out.println("----user1 = " + user);

            user.setId(107);
            user.setUsername("Honey");
            client.insert(user);

            System.out.println();
            List<User> userList = client.getAllUsers();
            userList.forEach(System.out::println);

            List<User> users = client.pageQueryUser(3, 10, "Jack");
            Assert.isTrue(users == null || users.size() < 1, "空");
        } catch (TException e) {
            System.out.println("==========发生异常");
//            assert (e.getMessage() != null);
//            assert (e.getMessage().contains("timeout"));
//            assert (e.getCause() != null);
//            assert (e.getCause() instanceof TTransportException);
//
//            assert (ExceptionUtils.getRootCause(e) instanceof SocketTimeoutException);
//
//            assert ("java.net.SocketTimeoutException: Read timed out".equals(e.getCause().getMessage()));
//            System.out.println("==== e= " + e);
//            System.out.println("超时异常");
            return;
        }

        //***************************

        System.exit(0);
    }
}

